<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head th:include="public/page_css::page_css ('查看质检规则明细')" />

<body>

<form class="layui-form layui-card-header  layui-card-header-auto" id="searchForm">
    <div class="layui-form-item" id="zjmx">
        <div class="layui-inline">
            <label class="layui-form-label">起始数量</label>
            <div class="layui-input-inline">
                <input type="text" name="qssl" id="qssl" placeholder="起始数量" autocomplete="off" class="layui-input "/>
            </div>
        </div>
        <div hidden><input type="text" class="layui-input" th:value="${id}" id="sszjgzid" name="sszjgzid"/></div>
        <div hidden><input type="text" name="sx" id="sx"/></div>
        <div class="layui-inline">
            <label class="layui-form-label">结束数量</label>
            <div class="layui-input-inline">
                <input type="text" name="jssl" id="jssl" placeholder="结束数量" autocomplete="off" class="layui-input "/>
            </div>
        </div>
        <div class="layui-form-item" style="margin: 1rem 0;position: absolute;top: 6.5rem;right: 10%;">
            <div class="layui-input-block">
                <a class="layui-btn" lay-submit id="addDetail">添加</a>
            </div>
        </div>
        <div id="bltable">
            <div class="layui-inline">
                <label class="layui-form-label">抽检比例(1-100%)</label>
                <div class="layui-input-inline">
                    <input type="text" name="bfb" id="bfb" placeholder="抽检比例" autocomplete="off" class="layui-input "/>
                </div>
            </div>
            <section class="panel panel-padding layui-card-body" id="table1div" >
                <div class="pro">
                    <table id="table1">
                    </table>
                </div>
            </section>
        </div>

        <div id="gdtable">
            <div class="layui-inline">
                <label class="layui-form-label">固定数量</label>
                <div class="layui-input-inline">
                    <input type="text" name="gdsl" id="gdsl"  placeholder="固定数量" autocomplete="off" class="layui-input "/>
                </div>
            </div>
            <section class="panel panel-padding layui-card-body" id="table2div" >
                <div class="pro">
                    <table id="table2">
                    </table>
                </div>
            </section>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" jq-filter="submit" lay-submit="" lay-filter="addMx">立即提交</button>
            <!--<button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
        </div>
    </div>

</form>
<input type="hidden" class="layui-input" th:value="${fs}" id="fs"/>
</body>
<div th:include="public/public_js::public_js"></div>

<script th:inline="javascript">
    $(function () {
    })

</script>
<script>
    layui.use(['myform','zzpUtils','form','jquery'],function () {
        var form = layui.form,
            zzpUtils = layui.zzpUtils,
            $ = layui.jquery;

        var newtableData;
        var sppendLen  = 0;
        if($("#fs").val()==1) {
            $('#bltable').css('display', 'block');
            $('#gdtable').css('display', 'none');
            var $table1 = zzpUtils.table({
                elem: '#table1',
                isApi: true,
                pagination: true,
                queryParams :queryParams1,
                url: '/wmsqualitytestingdetailed/getDetailList',
                columns: [/*{
                    field: '',
                    checkbox: true,
                },*/ {
                    field: 'sx',
                    title: '序号',
                }, {
                    field: 'qssl',
                    title: '起始数量'
                }, {
                    field: 'jssl',
                    title: '结束数量'
                }, {
                    field: 'bfb',
                    title: '抽检比例',
                    formatter: function (value, rowData, rowIndex) {
                        if (value && value != 0) {
                            return parseFloat(value) + '%';
                        } else {
                            return "0";
                        }
                        ;
                    },
                }]
            });
        }
        if($("#fs").val()==2) {
            $('#gdtable').css('display', 'block');
            $('#bltable').css('display', 'none');
            var $table2 = zzpUtils.table({
                elem: '#table2',
                isApi: true,
                pagination: true,
                queryParams :queryParams,
                url: '/wmsqualitytestingdetailed/getDetailList',
                columns: [/*{
                    field: '',
                    checkbox: true,
                }, */{
                    field: 'sx',
                    title: '序号',
                }, {
                    field: 'qssl',
                    title: '起始数量'
                }, {
                    field: 'jssl',
                    title: '结束数量'
                }, {
                    field: 'gdsl',
                    title: '固定数量'
                }]
            });
        }

        //得到查询的参数
        function queryParams(params) {
            var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                sszjgzid: $("#sszjgzid").val()
            };
            return temp;
        }
        //得到查询的参数
        function queryParams1(params) {
            var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                sszjgzid: $("#sszjgzid").val()
            };
            return temp;
        }

        //添加质检明细
        var index = 1;
        /* if(newtableData.length<1){*/
        $('#addDetail').click(function () {
            var values = $('#zjmx input[type="text"]');
            var datas = {};
            for (var ii = 0; ii < values.length; ii++) {
                var item = values[ii];
                datas[item.name] = item.value;
                $("#" + item.id).val('');
            }
            var datacopy = {};
            datacopy['qssl'] = datas['qssl'];
            datacopy['jssl'] = datas['jssl'];
            datacopy['bfb'] = datas['bfb'];
            datacopy['gdsl'] = datas['gdsl'];
            datacopy['sszjgzid'] = datas['sszjgzid'];
            datacopy['sx'] = datas['sx'];
            if ($('#fs').val() == 1) {
                var tabledata = $table1.bootstrapTable('getData');
                var lendata = tabledata[tabledata.length-1];
                if (tabledata.length === 0) {
                    var qssl = parseInt(datacopy['qssl']);
                    var jssl = parseInt(datacopy['jssl']);
                    var bfb=parseInt(datacopy['bfb']);
                    if(qssl <= jssl){
                        if( bfb >0 && bfb <100){
                            datacopy['sx'] = index;
                            $table1.bootstrapTable('append', datacopy);
                        }else {
                            layer.msg('抽检比例 介于0和100之间');
                        }
                    }else {
                        layer.msg('起始数量 不能大于 结束数量');
                    }
                } else {
                    var qssl = parseInt(datacopy['qssl']);
                    var jssl = parseInt(datacopy['jssl']);
                    var lastJssl=parseInt(lendata.jssl);
                    var sx= lendata.sx;
                    if (qssl >= lastJssl) {
                        if (qssl <= jssl) {
                            if (datacopy['bfb'] > 0 && datacopy['bfb'] < 100) {
                                datacopy['sx'] = sx + 1;
                                if(sppendLen === 0){
                                    newtableData = datacopy;
                                    $table1.bootstrapTable('append', datacopy);
                                    sppendLen=sppendLen+1;
                                }else{
                                    layer.msg('每次只能添加一条质检明细');
                                }

                            } else {
                                layer.msg('抽检比例 介于0和100之间');
                            }
                        } else {
                            layer.msg('起始数量 不能大于 结束数量');
                        }
                    } else {
                        layer.msg('输入的起始数量要大于前一条的结束数量！');
                    }

                }
            }
            if ($('#fs').val() == 2) {
                var tabledata2 = $table2.bootstrapTable('getData');
                var lendata2 = tabledata2[tabledata2.length-1];
                //var sx= lendata2.sx;
                //本次输入起始数量
                var qjcha =datacopy['jssl']-datacopy['qssl'];
                if (tabledata2.length === 0) {
                    var qssl = parseInt(datacopy['qssl']);
                    //本次输入的结束数量
                    var jssl = parseInt(datacopy['jssl']);
                    var gdsl = parseInt(datacopy['gdsl']);
                    if(qssl <= jssl){
                        if( gdsl <= qjcha){
                            datacopy['sx'] = index;
                            $table2.bootstrapTable('append', datacopy);
                        }else {
                            layer.msg('固定数量 不能大于 结束数量减去起始数量的值:'+qjcha);
                        }
                    }else {
                        layer.msg('起始数量 不能大于 结束数量');
                    }
                }else {
                    var qssl = parseInt(datacopy['qssl']);
                    //上条数据的结束数量
                    var jsslLast = parseInt(lendata2.jssl);
                    //本次输入的结束数量
                    var jssl = parseInt(datacopy['jssl']);
                    var gdsl = parseInt(datacopy['gdsl']);
                    if (qssl >= jsslLast) {
                        if(qssl <= jssl){
                            if( gdsl <= qjcha){
                                datacopy['sx'] = sx + 1;
                                if(sppendLen === 0){
                                    newtableData = datacopy;
                                    $table2.bootstrapTable('append', datacopy);
                                    sppendLen=sppendLen+1;
                                }else{
                                    layer.msg('每次只能添加一条质检明细');
                                }
                            }else {
                                layer.msg('固定数量 不能大于 结束数量减去起始数量的值:'+qjcha);
                            }
                        }else {
                            layer.msg('起始数量 不能大于 结束数量');
                        }
                    } else {
                        layer.msg('输入的起始数量要大于前一条的结束数量！');
                    }
                }
            }

        })
        /*}else{
            layer.msg('每次只能添加一条质检明细！');
        }*/

        form.on("submit(addMx)",function (data) {
            //console.log(newtableData);console.log('formdata', JSON.stringify(formdata));
            //var detailedList = [];
            var ddata = {};
            for (key in newtableData) {
                if (key !== '' && newtableData[key] !== '') {
                    ddata[key] = newtableData[key];
                }
            }
            //detailedList.push(ddata);
            zzpUtils.confirm("是否确定添加", function (index) {
                zzpUtils.action({
                    url: "/wmsqualitytestingdetailed/add",
                    data: JSON.stringify(ddata),
                    success: function (data) {
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index);
                            //再执行关闭
                            top.layer.msg(
                                data.title
                            );
                        }, 500);
                    }, error: function (xhr, textStatus) {
                        console.log(textStatus);
                        console.log(xhr);
                    }
                })
            })
            return false;
        });

    });
</script>
</html>